#!/bin/bash
# 安装Grafana

# 引入公共配置
source ./common.sh

# 确保应用目录存在
app_home

function install_grafana() {
    local appname="grafana-enterprise-${Version}-1.x86_64.rpm"
    
    echo "请确保已从浏览器下载 ${GRAFANA_NAME}${Version}-1.x86_64.rpm 并放在 ${APPS_HOME} 目录下"
    read -p "文件已准备好，请按回车键继续..."
    
    if [ ! -f "${APPS_HOME}/${appname}" ];then
        error_exit "未找到 ${APPS_HOME}/${appname} 文件"
    fi
    
    # 安装依赖
    yum install -y urw-fonts
    
    # 安装Grafana
    rpm -ivh ${APPS_HOME}/${appname}
    if [ $? -ne 0 ];then
        error_exit "Grafana安装失败"
    fi
    
    # 重启服务
    pkill grafana-server 2>/dev/null
    systemctl start grafana-server
    systemctl enable grafana-server
    
    if [ $? -eq 0 ];then
        success_msg "grafana ${Version} 安装成功"
        systemctl status grafana-server
        echo "默认用户名和密码: admin/admin"
        echo "访问地址: http://服务器IP:3000"
    else
        error_exit "grafana ${Version} 启动失败"
    fi
}

echo "开始安装Grafana..."

read -p "输入需要的grafana版本(默认9.0.7): " Version
if [ -z "${Version}" ];then
    Version="9.0.7"
    echo "使用默认版本: $Version"
fi

install_grafana
